/**
 * @Copyright：<a href="http://www.wisesoft.net.cn">四川川大智胜系统集成有限公司</a>
 * @Author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
 * @CreateTime: 2022年08月06日  17:45
 */
package com.fxsen.commons.utils;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 日期时间工具
 *@Author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
 *@CreateTime: 2022年08月06日  17:45
 */
public class DateUtils {
    private static Calendar calendar = Calendar.getInstance();
    /**
     * 字符串形式日期格式（yyyy年MM月dd日）
     */
    public static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");

    /**
     * 字符串形式日期时间格式（yyyy年MM月dd日 HH:mm:ss）
     */
    public static final SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");

    /**
     * 字符串形式时间格式（HH:mm:ss）
     */
    public static final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");

    /**
     * 字符串时间格式年（yyyy年）
     */
    public static final SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy年");

    /**
     * 字符串时间格式年月（yyyy年MM月）
     */
    public static final SimpleDateFormat yearMonthFormat = new SimpleDateFormat("yyyy年MM月");

    /**
     * Flash字符串形式日期格式（yyyy,MM,dd,HH,mm,ss）
     */
    public static final SimpleDateFormat DATETIMEFORMATFLASH = new SimpleDateFormat("yyyy,MM,dd,HH,mm,ss");

    public static final String DATE_TIME_FORMAT = "yyyy年MM月dd日 HH:mm:ss";
    /**
     * 日期格式年月日（yyyy-MM-dd）
     */
    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
    /**
     * 日期格式年月（yyyy-MM）
     */
    public static final String DEFAULT_DATE_FORMAT1 = "yyyy-MM";
    /**
     * 日期格式月-日（MM-dd）
     */
    public static final String DEFAULT_DATE_FORMAT_MM_DD = "MM-dd";
    /**
     * 日期格式年月日时分（yyyy-MM-dd HH:mm）
     */
    public static final String DEFAULT_DATETIME_FORMAT_EX = "yyyy-MM-dd HH:mm";
    /**
     * 日期格式年月日时分秒（yyyy-MM-dd HH:mm:ss）
     */
    public static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    /**
     * yyyyMMddHHmmss
     */
    public static final String DEFAULT_DATETIME_FORMAT2 = "yyyyMMddHHmmss";
    /**
     * HH:mm:ss
     */
    public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
    /**
     * yyyy/MM/dd HH:mm:ss
     */
    public static final String DEFAULT_DATETIME_FORMAT3 = "yyyy/MM/dd HH:mm:ss";
    /**
     * yyyy/MM/dd HH:mm:ss.S
     */
    public static final String DEFAULT_DATETIME_FORMAT4 = "yyyy/MM/dd HH:mm:ss.S";
    /**
     * yyyy/MM/dd HH:mm:ss.S
     */
    public static final String DEFAULT_DATETIME_FORMAT5 = "yyyy/MM/dd HH:mm:ss.S";
    /**
     * yyyy/MM/dd HH:mm
     */
    public static final String DEFAULT_DATETIME_FORMAT6 = "yyyy/MM/dd HH:mm";
    /**
     * yyyy/MM/dd
     */
    public static final String DEFAULT_DATETIME_FORMAT7 = "yyyy/MM/dd";
    /**
     * yyyyMMddHH
     */
    public static final String DEFAULT_DATETIME_FORMAT8 = "yyyyMMddHH";
    /**
     * 日期格式年月日时分秒（yyyy-MM-dd HH）
     */
    public static final String DEFAULT_DATETIME_FORMAT9 = "yyyy-MM-dd HH";
    /**
     * yyyyMMdd
     */
    public static final String DEFAULT_DATETIME_FORMAT10 = "yyyyMMdd";

    /**
     * 显示月-日 时间
     */
    public static final String DEFAULT_DATETIME_FORMAT11 = "MM-dd HH";
    /**
     * yyyy/MM/dd
     */
    public static final String DEFAULT_DATETIME_FORMAT12 = "MM/dd/yyyy HH";
    /**
     * yyyyMMddHHmmssS
     */
    public static final String DEFAULT_DATETIME_FORMAT13 = "yyyyMMddHHmmssS";
    public static final String DEFAULT_DATETIME_FORMAT14 = "yyyy年MM月dd日HH时mm分";
    /**
     * 日期格式小时（HH）
     */
    public static final String DATE_FORMAT_HOUR = "HH:00";

    /** 一秒钟的毫秒数 */
    public static final int ONE_SECOND_MILLIOS = 1000;
    /** 一分钟的毫秒数 */
    public static final int ONE_MINUTE_MILLIONS = 60 * ONE_SECOND_MILLIOS;
    /** 一小时的毫秒数 */
    public static final int ONE_HOUR_MILLIONS = 60 * ONE_MINUTE_MILLIONS;
    /** 一天的毫秒数 */
    public static final int ONE_DAY_MILLIONS = 24 * ONE_HOUR_MILLIONS;

    private static Map<String, String> ENG_MONTH_MAP = new HashMap<String, String>() {
        /**
         *
         */
        private static final long serialVersionUID = -215126330932792283L;

        {
            put("JAN/", "01");
            put("FEB/", "02");
            put("MAR/", "03");
            put("APR/", "04");
            put("MAY/", "05");
            put("JUN/", "06");
            put("JUL/", "07");
            put("AUG/", "08");
            put("SEP/", "09");
            put("OCT/", "10");
            put("NOV/", "11");
            put("DEC/", "12");
        }
    };

    /**
     * 将日期转成String类型
     *
     * @param date
     * @param format
     *            要格式化的格式
     * @return
     */
    public static String format(Date date, String format) {
        if (date == null) {
            return "null";
        }
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(date);
    }

    /**
     * 格式化一个时间段
     *
     * @param time
     *            所经历时间的毫秒数
     * @param format
     *            要格式化的格式
     * @return
     */
    public static String formatDuration(long time, String format) {
        String result = new String(format);
        int hour = (int) (time / ONE_HOUR_MILLIONS);
        int minute = (int) ((time - ONE_HOUR_MILLIONS) / (ONE_MINUTE_MILLIONS));
        int second = (int) ((time - ONE_HOUR_MILLIONS - (minute * ONE_MINUTE_MILLIONS)) / ONE_SECOND_MILLIOS);
        int millisecond = (int) (time % 1000);

        result = result.replace("hh", String.valueOf(hour));
        result = result.replace("mm", String.valueOf(minute));
        result = result.replace("ss", String.valueOf(second));
        result = result.replace("SSS", String.valueOf(millisecond));

        return result;
    }

    /**
     * 将 <code>java.util.Date</code> 转换为 <code>java.sql.Date</code>
     *
     * @param util
     *            .date
     * @return sql.Date
     */
    public static java.sql.Date toSqlDate(Date date) {
        if (date != null) {
            return new java.sql.Date(date.getTime());
        }

        return null;
    }

    /**
     * 将 <code>java.util.Date</code> 转换为 <code>java.sql.Timestamp</code>
     *
     * @param util
     *            .date
     * @return sql.Timestamp
     */
    public static java.sql.Timestamp toTimestamp(Date date) {
        if (date != null) {
            return new java.sql.Timestamp(date.getTime());
        }

        return null;
    }

    /**
     * 转换字符串到日期格式
     *
     * @param string
     * @param format
     *            日期格式
     * @return
     */
    public static Date parseDate(String string, String format) {
        Date date = null;
        try {
            if (string != null) {
                SimpleDateFormat formatter = new SimpleDateFormat(format);
                date = formatter.parse(string);
            }
        } catch (Exception ex) {
        }
        return date;
    }

    /**
     * 根据输入的时间字符串转成date 时间字符串是："2011-08-08 11:20:36" "2011-08-08 11:20"
     * "2011-08-08";
     *
     * @param string
     * @return
     */
    public static Date parseDate(String string) {
        String format = DEFAULT_DATE_FORMAT;

        if (string.matches("\\d{2,4}\\-\\d{1,2}\\-\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}"))
            format = DEFAULT_DATETIME_FORMAT;
        if (string.matches("\\d{2,4}\\-\\d{1,2}\\-\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}.0"))
            format = DEFAULT_DATETIME_FORMAT;

        if (string.matches("\\d{2,4}\\-\\d{1,2}\\-\\d{1,2} \\d{1,2}:\\d{1,2}"))
            format = DEFAULT_DATETIME_FORMAT_EX;

        return parseDate(string, format);
    }

    /**
     * 自动解析字符串转成date
     *
     * @param string
     * @return
     */
    public static Date parseDate1(String string) {
        Date date = null;
        date = parseDate(string, DEFAULT_DATE_FORMAT);
        if (date == null) {
            date = parseDate(string, DEFAULT_DATETIME_FORMAT_EX);
        }
        if (date == null) {
            date = parseDate(string, DEFAULT_DATETIME_FORMAT);
        }
        if (date == null) {
            date = parseDate(string, DEFAULT_DATETIME_FORMAT2);
        }
        if (date == null) {
            date = parseDate(string, DEFAULT_DATETIME_FORMAT3);
        }
        if (date == null) {
            date = parseDate(string, DEFAULT_DATETIME_FORMAT4);
        }
        if (date == null) {
            date = parseDate(string, DEFAULT_DATETIME_FORMAT5);
        }
        if (date == null) {
            date = parseDate(string, DEFAULT_DATETIME_FORMAT6);
        }
        if (date == null) {
            date = parseDate(string, DEFAULT_DATETIME_FORMAT7);
        }
        return date;
    }

    /**
     * 比较s1和s2转换成日期后比较的结果， s1 和 s2 相同返回 0 ， s1 在 s2 之后返回 1, 反之返回 -1
     *
     * @param s1
     * @param s2
     * @return
     */
    public static int compareDate(String s1, String s2) {
        try {

            Date d1 = parseDate(s1);
            Date d2 = parseDate(s2);
            return d1.compareTo(d2);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return -1;
    }

    /**
     * 比较d1和d2日期的结果， d1和d2相同返回 0
     *
     * @param d1
     * @param d2
     * @return
     */
    public static int compareDate(Date d1, Date d2) {
        try {
            return d1.compareTo(d2);
        } catch (Exception ex) {

        }
        return -1;
    }

    /**
     * 根据long得到Calendar对象
     *
     * @param time
     *            时间long
     * @return
     */
    private static Calendar getCalendar(long time) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(time);
        return cal;
    }

    /**
     * 返回以秒为单位的两个时间的差
     *
     * @param date1
     * @param date2
     * @return
     */
    public static double getDiffInSecond(Date date1, Date date2) {
        long diff = date2.getTime() - date1.getTime();
        return DecimalUtils.round(diff * 1.0 / 1000, 2);
    }

    /**
     * 得到指定日期的年
     *
     * @param date
     * @return
     */
    public static int getYear(Date date) {
        Calendar cal = getCalendar(date.getTime());
        return cal.get(Calendar.YEAR);
    }

    /**
     * 得到指定日期的月
     *
     * @param date
     * @return
     */
    public static int getMonth(Date date) {
        Calendar cal = getCalendar(date.getTime());
        return cal.get(Calendar.MONTH) + 1;
    }

    /**
     * 得到指定日期的日
     *
     * @param date
     * @return
     */
    public static int getDay(Date date) {
        Calendar cal = getCalendar(date.getTime());
        return cal.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 得到指定日期的小时
     *
     * @param date
     * @return
     */
    public static int getHour(Date date) {
        Calendar cal = getCalendar(date.getTime());
        return cal.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 得到指定日期的分钟
     *
     * @param date
     * @return
     */
    public static int getMinute(Date date) {
        Calendar cal = getCalendar(date.getTime());
        return cal.get(Calendar.MINUTE);
    }

    /**
     * 得到指定日期的秒
     *
     * @param date
     * @return
     */
    public static int getSecond(Date date) {
        Calendar cal = getCalendar(date.getTime());
        return cal.get(Calendar.SECOND);
    }

    /**
     * 得到当前字符串形式日期 格式：yyyy年mm月dd日
     *
     * @return String 当前字符串形式日期（yyyy年mm月dd日）
     */
    public static String getCurrDate() {
        return dateFormat.format(new Date());
    }

    /**
     * 得到当前字符串形式日期 格式由参数pattern决定
     *
     * @param pattern
     *            时间格式
     * @return String 格式由参数pattern决定
     */
    public static String getCurrDate(String pattern) {
        Date utilDate = new Date(System.currentTimeMillis());
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        return df.format(utilDate);
    }

    /**
     * 得到当前日期字符串形式
     *
     * @return String yyyyMMddHHmmssS
     */
    public static String getCurrDate2() {
        Date utilDate = new Date(System.currentTimeMillis());
        SimpleDateFormat df = new SimpleDateFormat(DEFAULT_DATETIME_FORMAT13);
        return df.format(utilDate);
    }

    /**
     * 得到当前字符串形式时间 格式：yyyy年mm月dd日 hh:MM:ss
     *
     * @return String 当前字符串形式时间（yyyy年mm月dd日 hh:MM:ss）
     */
    public static String getCurrTime() {
        return datetimeFormat.format(new Date());
    }

    /**
     * 得到当前字符串形式时间 格式由参数pattern决定
     *
     * @param pattern
     *            时间格式
     * @return String 格式由参数pattern决定
     */
    public static String getCurrTime(String pattern) {
        Date utilDate = new Date(System.currentTimeMillis());
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        return df.format(utilDate);
    }

    /**
     * 得到不包含日期的当前时间
     *
     * @return String 不包含日期的当前时间（HH:mm:ss）
     */
    public static String getCurrOnlyTime() {
        return timeFormat.format(new Date());
    }

    /**
     * 格式化指定时间对象为指定字符串形式时间
     *
     * @param date
     *            时间对象
     * @return 字符串形式时间格式为（yyyy年MM月dd日 HH:mm:ss）
     */
    public static String formateTime(Date date) {
        return datetimeFormat.format(date);
    }

    /**
     * 格式化指定日期对象为指定字符串形式时间
     *
     * @return String 时间格式（HH:mm:ss）
     */
    public static String formatOnlyTime(Date date) {
        return timeFormat.format(date);
    }

    /**
     * 格式化指定时间对象为指定字符串形式时间
     *
     * @param date
     *            时间对象
     * @return 字符串形式时间格式为：（yyyy年MM月dd日）
     */
    public static String formateDate(Date date) {
        return dateFormat.format(date);
    }

    /**
     * 将字符串日期转换成时间对象
     *
     * @param dateStr
     *            字符串日期 格式为：（2007年04月06日 12:12:32）或（2007年04月06日）
     * @return Date 格式为（Fri Apr 06 00:00:00 CST 2007）
     * @throws ParseException
     *             e
     */
    // public static Date parseDate1(String dateStr) throws ParseException {
    // return dateFormat.parse(dateStr);
    // }

    /**
     * 将字符串日期时间转换成时间对象
     *
     * @param dateTimeStr
     *            字符串日期时间格式为：（yyyy年MM月dd日 HH:mm:ss）
     * @return Date 格式为：（Fri Apr 06 12:12:32 CST 2007）
     * @throws ParseException
     *             e
     */
    public static Date parseDateTime(String dateTimeStr) throws ParseException {
        return datetimeFormat.parse(dateTimeStr);
    }

    /**
     * 得到年字符串
     *
     * @param date
     * @return 字符串格式年（yyyy年）
     */
    public static String getYear1(Date date) {
        return yearFormat.format(date);
    }

    /**
     * 得到年月字符串
     *
     * @param date
     * @return 字符串格式年月（yyyy年MM月）
     */
    public static String getYearMonth(Date date) {
        return yearMonthFormat.format(date);
    }

    /**
     * 得到指定日期隶属年内第几周
     *
     * @param date
     *            指定日期
     * @return 指定日期隶属年内第几周 格式为：（2007年15周）
     */
    public static String getYearWeek(Date date) {
        return getYearWeek(date, WEEK_FORMAT_1);
    }

    /**
     * 周格式-xxxx年xx周
     */
    public static final String WEEK_FORMAT_1 = "xxxx年xx周";
    /**
     * 周格式-xxxxxx
     */
    public static final String WEEK_FORMAT_2 = "xxxxxx";

    /**
     * 根据type取返回值,需要什么,自己扩展
     *
     * @param date
     * @param type
     * @return Description: Date: 2014年11月29日 Time: 下午8:48:39
     * @author fxsen
     */
    public static String getYearWeek(Date date, String type) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        String year = "" + calendar.get(Calendar.YEAR);
        int temWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        int month = calendar.get(Calendar.MONTH);
        if (temWeek == 1 && month == 11) // 如果年周是第1周而且是12月份则实际是年的最后一周，则此处需要调整，关于此问题尚未清楚，如果谁清楚此问题请Email:huangj8848@gmail.com,不甚感激。
        {
            calendar.add(Calendar.DAY_OF_YEAR, -7);
            temWeek = calendar.get(Calendar.WEEK_OF_YEAR) + 1;
        }

        String week = "" + temWeek;

        // 拼字符串返回
        year = "0000".substring(0, 4 - year.length()) + year;
        week = "00".substring(0, 2 - week.length()) + week;
        switch (type) {
            case WEEK_FORMAT_1:
                return year + "年" + week + "周";
            case WEEK_FORMAT_2:
                return year + week;
            default:
                return year + "年" + week + "周";
        }
    }

    /**
     * 得到指定日期隶属年内第几季度
     *
     * @param date 指定日期
     * @return 指定日期隶属年内第几季度 格式为：（2007年第4季度）
     */
    public static String getYearQuarter(Date date) {
        return getYearQuarter(date, QUARTER_1);
    }

    /**
     * 季度格式-xxxx年第x季度
     */
    public static final String QUARTER_1 = "xxxx年第x季度";
    /**
     * 季度格式-xxxxxx
     */
    public static final String QUARTER_2 = "xxxxxx";
    /**
     *
     * @param date
     * @param type
     * @return
     * Description: 返回季度数
     * Date: 2015年7月13日
     * Time: 下午6:28:22
     * @author lin.yuanhong
     */
    public static int getQuarterInYear(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH);
        int curMonth = month + 1;
        if(curMonth <= 3){
            return 1;
        }else if(curMonth > 3 && curMonth <= 6){
            return 2;
        }else if(curMonth > 6 && curMonth <= 9){
            return 3;
        }else{
            return 4;
        }
    }
    public static String getYearQuarter(Date date, String type) {
        String quarter = "";
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        String year = "" + calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int curMonth = month + 1;
        // System.out.println("--------:" + curMonth);
        if (curMonth <= 3)
            quarter = "1";
        if (curMonth > 3 && curMonth <= 6)
            quarter = "2";
        if (curMonth > 6 && curMonth <= 9)
            quarter = "3";
        if (curMonth > 9 && curMonth <= 12)
            quarter = "4";
        year = "0000".substring(0, 4 - year.length()) + year;
        switch (type) {
            case QUARTER_1:
                return year + "年" + "第" + quarter + "季度";
            case QUARTER_2:
                quarter = "00".substring(0, 2 - quarter.length()) + quarter;// 这里是特殊需求
                return year + "年" + quarter + "季";
            default:
                return year + "年" + "第" + quarter + "季度";
        }

    }

    /**
     * 当前时间的上一季度
     *
     * @param date
     * @param type
     * @return Description: Date: 2015年1月22日 Time: 下午12:02:31
     * @author fxsen
     */
    public static String getYearPreQuarter(Date date, String type) {
        String quarter = "";
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        String year = "" + calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int curMonth = month + 1;
        // System.out.println("--------:" + curMonth);
        if (curMonth <= 3) {
            quarter = "4";
            year = "" + (Integer.parseInt(year) - 1);
        }
        if (curMonth > 3 && curMonth <= 6)
            quarter = "1";
        if (curMonth > 6 && curMonth <= 9)
            quarter = "2";
        if (curMonth > 9 && curMonth <= 12)
            quarter = "3";
        year = "0000".substring(0, 4 - year.length()) + year;
        switch (type) {
            case QUARTER_1:
                return year + "年" + "第" + quarter + "季度";
            case QUARTER_2:
                quarter = "00".substring(0, 2 - quarter.length()) + quarter;// 这里是特殊需求
                return year + "年" + quarter + "季";
            default:
                return year + "年" + "第" + quarter + "季度";
        }
    }

    /**
     * 当前时间的下一季度
     *
     * @param date
     * @param type
     * @return Description: Date: 2015年1月22日 Time: 下午12:02:31
     * @author fxsen
     */
    public static String getYearNextQuarter(Date date, String type) {
        String quarter = "";
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        String year = "" + calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int curMonth = month + 1;
        // System.out.println("--------:" + curMonth);
        if (curMonth <= 3)
            quarter = "2";
        if (curMonth > 3 && curMonth <= 6)
            quarter = "3";
        if (curMonth > 6 && curMonth <= 9)
            quarter = "4";
        if (curMonth > 9 && curMonth <= 12) {
            quarter = "1";
            year = "" + (Integer.parseInt(year) + 1);
        }
        year = "0000".substring(0, 4 - year.length()) + year;
        switch (type) {
            case QUARTER_1:
                return year + "年" + "第" + quarter + "季度";
            case QUARTER_2:
                quarter = "00".substring(0, 2 - quarter.length()) + quarter;// 这里是特殊需求
                return year + "年" + quarter + "季";
            default:
                return year + "年" + "第" + quarter + "季度";
        }
    }
    /**
     * 得到一年中周的开始时间和结束时间
     *
     * @param year
     *            指定年
     * @param week
     *            指定周(以年计算)
     * @return 包含开始时间和结束时间的date数组
     */
    public static Date[] getAreaOfWeek(int year, int week) {
        Date dt1 = null;
        Date dt2 = null;
        Date[] dates = new Date[2];
        Calendar cal = Calendar.getInstance(Locale.CHINA);
        cal.set(year, 0, 1, 0, 0, 0);

        int maxDay = cal.getActualMaximum(Calendar.DAY_OF_YEAR);
        for (int i = 0; i < maxDay; i++) {
            if (cal.get(Calendar.WEEK_OF_YEAR) == week) {
                if (dt1 == null)
                    dt1 = cal.getTime();
                dt2 = cal.getTime();
            }
            if (cal.get(Calendar.WEEK_OF_YEAR) > week)
                break;
            cal.add(Calendar.DAY_OF_YEAR, 1);
        }

        dates[0] = dt1;
        dates[1] = dt2;
        return dates;
    }

    /**
     * 判断闰年
     *
     * @param year
     *            年
     * @return 如果是闰年，返回true,否则返回false
     */
    public static boolean isLeapYear(int year) {
        if ((year % 4 == 0) && (year % 100 != 0))
            return true;
        else {
            if ((year % 100 == 0) && (year % 400 == 0))
                return true;
            else
                return false;
        }
    }

    /**
     * Description: 判断两个日期是否是同一天
     *
     * @author fxsen 2014-9-22 上午11:56:35
     */
    public static boolean isSameDay(Date day1, Date day2) {
        if (day1 == null || day2 == null) {
            return false;
        }
        String strDay1 = format(day1, DEFAULT_DATE_FORMAT);
        String strDay2 = format(day2, DEFAULT_DATE_FORMAT);
        if (strDay1.equals(strDay2)) {
            return true;
        }
        return false;
    }

    /**
     * 获取 时：分：秒所代表的毫秒数
     *
     * @param hourMinuteSecond
     *            12:14:16
     * @return long毫秒数
     */
    public static long getHourMinuteSecond(String hourMinuteSecond) {

        String simpleDateStr = getCurrDate(); // yyyy年MM月dd日
        String dateStr = simpleDateStr + " " + hourMinuteSecond; // yyyy年MM月dd日
        // HH:mm:ss

        long timeMillions = 0;
        long dateMillions = 0;
        try {
            timeMillions = (datetimeFormat.parse(dateStr)).getTime();
            dateMillions = (dateFormat.parse(simpleDateStr)).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return timeMillions - dateMillions;
    }

    /**
     * 根据提供的 时：分：秒重新生成一个当前日期的时间，并返回新生成的日期的毫秒数
     *
     * @param hourMinuteSecond
     *            12:14:16
     * @return long单位毫秒
     */
    public static long getCurrentTimeByHourMinuteSecond(String hourMinuteSecond) {

        String simpleDateStr = getCurrDate(); // yyyy年MM月dd日
        String dateStr = simpleDateStr + " " + hourMinuteSecond; // yyyy年MM月dd日
        // HH:mm:ss
        long newTimeMillions = 0;
        try {
            newTimeMillions = (datetimeFormat.parse(dateStr)).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return newTimeMillions;
    }

    /**
     * 获取N天以前的日期 yyyy年MM月dd日 HH:mm:ss 2006年11月01日 15:51:14
     *
     * @param N
     * @return 返回该日期 2006年11月01日 15:51:14(时分秒不变)
     */
    public static String getNDayBeforeDateStr(int N) {

        long Current_Time_Millions = System.currentTimeMillis();

        // String currentDate = datetimeFormat.format(new
        // Date(Current_Time_Millions));
        String nDayBefore = datetimeFormat.format(new Date(Current_Time_Millions - ONE_DAY_MILLIONS * N));

        // System.out.println("currentDate: " + currentDate + " nDayBefore: " +
        // nDayBefore);
        return nDayBefore;
    }

    /**
     * 获取 N 天以前的日期,该日期只是年月日 2006年10月27日
     *
     * @param N
     * @return 返回该日期只是年月日 2006年10月27日
     */
    public static String getNDayBeforeOnlyDateStr(int N) {
        String nDayBeforeDateTime = getNDayBeforeDateStr(N);

        // System.out.println(nDayBeforeDateTime.substring(0,nDayBeforeDateTime.lastIndexOf("日")
        // + 1));
        // System.out.println("nDayBeforeOnlyDay: " +
        // nDayBeforeDateTime.split(" ")[0]);
        return nDayBeforeDateTime.split(" ")[0];
    }

    /**
     * 根据日期参数获取这个日期当前是星期几 例如: 2006年11月01日 17:12:54 当前是 星期三
     *
     * @param dateStr
     *            yyyy年MM月dd日 HH:mm:ss 或 yyyy年MM月dd日
     * @return 字符串 星期几
     */
    public static String getDayOfWeek(String dateStr) {
        Date date = null;
        Calendar calendar = null;
        String dayOfWeek = "";
        if (dateStr.lastIndexOf(" ") == -1) {
            /*
             * 说明传递的参数形如: yyyy年MM月dd日
             */
            dateStr += " 00:00:00";
        }
        try {
            date = datetimeFormat.parse(dateStr);
            calendar = Calendar.getInstance();
            calendar.setTime(date);
            switch (calendar.get(Calendar.DAY_OF_WEEK) - 1) {
                case 0:
                    dayOfWeek = "星期日";
                    break;
                case 1:
                    dayOfWeek = "星期一";
                    break;
                case 2:
                    dayOfWeek = "星期二";
                    break;
                case 3:
                    dayOfWeek = "星期三";
                    break;
                case 4:
                    dayOfWeek = "星期四";
                    break;
                case 5:
                    dayOfWeek = "星期五";
                    break;
                case 6:
                    dayOfWeek = "星期六";
                    break;
                default:
                    dayOfWeek = "星期日";
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dayOfWeek;
        // return "星期日";
    }

    /**
     * 获取两个时间字符串之间的时间差,该差是相差的毫秒数
     *
     * @param dateStr1
     * @param dateStr2
     * @return long单位毫秒
     *
     */
    public static long getDifferDateTimeslot(String dateStr1, String dateStr2) {
        if (dateStr1.lastIndexOf(" ") == -1) {
            /*
             * 说明传递的参数形如: yyyy年MM月dd日
             */
            dateStr1 += " 00:00:00";
        }
        if (dateStr2.lastIndexOf(" ") == -1) {
            /*
             * 说明传递的参数形如: yyyy年MM月dd日
             */
            dateStr2 += " 00:00:00";
        }
        try {
            Date date1 = datetimeFormat.parse(dateStr1);
            Date date2 = datetimeFormat.parse(dateStr2);
            if (date1.getTime() >= date2.getTime())
                return date1.getTime() - date2.getTime();
            else
                return date2.getTime() - date1.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 得到给定日期之后数个月的日期字符串
     *
     * @param date
     *            给定日期(2011年5月6日)
     * @param monthCount
     *            月数
     * @return String 日期（年月日）日不变
     * @throws Exception
     *             e
     */
    public static String getDistanceDate(String date, int monthCount) throws Exception {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(parseDate(date));
        ElapsedTime et = new ElapsedTime();
        return formateDate(et.getAfterMonth(gc, monthCount));
    }

    /**
     * 返回前几天或后几天的时间
     * @param date 日期
     * @param day 整数返回后几天,负数返回前几天
     * @return
     */
    public static Date getDay(Date date, int day) {
        calendar.setTime(date);
        calendar.add(Calendar.DATE, day);
        return calendar.getTime();
    }

    /**
     * 获取指定小时后的日期
     *
     * @param date
     * @param hourCount
     *            小时数(该数可以小于0，当小于时，返回指定时间几小时之前的时间)
     * @return
     */
    public static Date getDateAfterHour(Date date, int hourCount) {
        return new Date(date.getTime() + ONE_HOUR_MILLIONS * hourCount);
    }

    /**
     * 获取指定分钟后的日期
     *
     * @param date
     * @param minuteCount
     *            分钟数(该数可以小于0，当小于时，返回指定时间几分钟之前的时间)
     * @return
     */
    public static Date getDateAfterMinute(Date date, double minuteCount) {
        return new Date((long) (date.getTime() + ONE_MINUTE_MILLIONS * minuteCount));
    }

    /**
     * 格式化日期 2007年01月01日 转换为 07/01/01
     *
     * @param date
     * @return 返回日期字符串（yy/MM/dd格式）
     * @throws Exception
     */
    public static String formateStringDate(String date) throws Exception {
        Date d = dateFormat.parse(date);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yy/MM/dd");
        return dateFormat.format(d);
    }

    /**
     * 格式化日期 2007年01月01日 转换为 07-01-01
     *
     * @param date
     *            2007年01月01日
     * @param formateType
     *            yyyy-MM-dd
     * @return 返回格式化后日期字符串（2007-01-01）
     */
    public static String formateStringDateByType(String date, String formateType) throws Exception {
        if (date.lastIndexOf(" ") != -1) {
            date = date.substring(0, date.lastIndexOf(" "));
        }
        Date d = dateFormat.parse(date);
        SimpleDateFormat dateFormat = new SimpleDateFormat(formateType);
        return dateFormat.format(d);
    }

    /**
     * 格式化日期 2007年01月01日 转换为 2007-01-01
     *
     * @param date
     *            2007年01月01日
     * @param sourceType
     *            例如:yyyy年MM月dd日
     * @param formateType
     *            例如:yyyy-MM-dd
     * @return 根据formateType返回。如：yyyy-MM-dd则返回2007-01-01
     * @throws ParseException
     */
    public static String formateStringDate2(String date, String sourceType, String formateType) throws ParseException {
        SimpleDateFormat sourceFormat = new SimpleDateFormat(sourceType);
        // /System.out.println(date);
        Date d = sourceFormat.parse(date);
        SimpleDateFormat dateFormat = new SimpleDateFormat(formateType);
        return dateFormat.format(d);
    }

    /**
     * 格式化日期格式
     *
     * @param date
     * @param f
     *            格式 例如:yyyy-MM-dd
     * @return 格式化后时间字符串
     */
    public static String getDateAsString(Date date, String f) {
        SimpleDateFormat format = new SimpleDateFormat(f);
        return format.format(date);
    }

    /**
     * 得到Flash字符串形式日期格式（yyyy,MM,dd,HH,mm,ss）
     *
     * @param date
     * @return 返回Flash字符串形式日期格式（2011,07,22,10,40,12）
     */
    public static String getDateAsFlash(Date date) {
        return DATETIMEFORMATFLASH.format(date);
    }

    /**
     * 根据生日返回年龄
     *
     * @param birthday
     * @return int 如果birthday为null返回0
     */
    public static int getAge(Date birthday) {
        if (birthday != null) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(birthday);
            int yearBirth = cal.get(Calendar.YEAR);

            Calendar cal2 = Calendar.getInstance();
            int yearNow = cal2.get(Calendar.YEAR);

            cal2.set(Calendar.YEAR, yearBirth);

            if (cal2.before(cal)) {
                return yearNow - yearBirth - 1;
            }
            return yearNow - yearBirth;
        } else {
            return 0;
        }
    }

    /**
     * 根据年龄返回生日(月日为当天的月日)
     *
     * @param age
     * @return 返回Date(Sun Jul 22 10:25:05 CST 1979)当天的月日时
     */
    public static Date getBirthDay(int age) {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR) - age;
        cal.set(Calendar.YEAR, year);
        return cal.getTime();
    }

    /**
     * 取得指定时间和当前时间相差的间隔
     *
     * @param messageTime
     *            (1311301838034L)
     * @return 返回时间差N分钟前、N（一到二十三）小时前、N（1到29）天前、一个月前、两个月前、三个月前、更早以前
     */
    // 相差一天是（24*60*60*1000）
    public static String getTimeInterval(long messageTime) {
        // /System.out.println(new Date().getTime());
        long interval = System.currentTimeMillis() - messageTime;
        long day = interval / ONE_DAY_MILLIONS;
        String time = null;
        if (day >= 0 && day < 1) {

            int hour = (int) interval / ONE_HOUR_MILLIONS;
            switch (hour) {
                case 0:
                    time = interval / ONE_MINUTE_MILLIONS + "分钟前";
                    break;
                case 1:
                    time = "一小时前";
                    break;
                case 2:
                    time = "两小时前";
                    break;
                case 3:
                    time = "三小时前";
                    break;
                case 4:
                    time = "四小时前";
                    break;
                case 5:
                    time = "五小时前";
                    break;
                case 6:
                    time = "六小时前";
                    break;
                case 7:
                    time = "七小时前";
                    break;
                case 8:
                    time = "八小时前";
                    break;
                case 9:
                    time = "九小时前";
                    break;
                case 10:
                    time = "十小时前";
                    break;
                case 11:
                    time = "十一小时前";
                    break;
                case 12:
                    time = "十二小时前";
                    break;
                case 13:
                    time = "十三小时前";
                    break;
                case 14:
                    time = "十四小时前";
                    break;
                case 15:
                    time = "十五小时前";
                    break;
                case 16:
                    time = "十六小时前";
                    break;
                case 17:
                    time = "十七小时前";
                    break;
                case 18:
                    time = "十八小时前";
                    break;
                case 19:
                    time = "十九小时前";
                    break;
                case 20:
                    time = "二十小时前";
                    break;
                case 21:
                    time = "二一小时前";
                    break;
                case 22:
                    time = "二十二小时前";
                    break;
                case 23:
                    time = "二十三小时前";
                    break;
                default:
                    break;
            }
        } else if (day >= 1 && day < 30) {
            time = interval / ONE_DAY_MILLIONS + "天前";
        } else if (day >= 30 && day < 60) {
            time = "一个月前";
        } else if (day >= 60 && day < 90) {
            time = "二个月前";
        } else if (day >= 90 && day < 120) {
            time = "三个月前";
        } else if (day >= 120) {
            time = "更早以前";
        }
        return time;
    }

    /**
     * 获取当月的 天数
     */
    public static int getCurrentMonthDay() {

        Calendar a = Calendar.getInstance();
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     *
     * Description: 获取当前的年份
     *
     * @author fxsen 2014-6-9 下午7:08:03
     */
    public static int getCurrentYear() {
        Calendar a = Calendar.getInstance();
        a.setTime(new Date());
        return a.get(Calendar.YEAR);
    }

    /**
     *
     * Description: 获取当前的月份
     *
     * @author fxsen 2014-6-10 上午11:53:59
     */
    public static int getCurrentMonth() {
        Calendar a = Calendar.getInstance();
        a.setTime(new Date());
        return a.get(Calendar.MONTH) + 1;
    }
    /**
     * 获得当前时间前一月
     * @return
     */
    public static int getBeforeMonth() {
        Calendar a = Calendar.getInstance();
        a.setTime(new Date());
        a.add(Calendar.MONTH, -1);
        return a.get(Calendar.MONTH) + 1;
    }
    /**
     * 获得当前时间后一月
     * @return
     */
    public static int getAfterMonth() {
        Calendar a = Calendar.getInstance();
        a.setTime(new Date());
        a.add(Calendar.MONTH, 1);
        return a.get(Calendar.MONTH) + 1;
    }

    /**
     * 根据年 月 获取对应的月份 天数
     */
    public static int getDaysByYearMonth(int year, int month) {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * Description: 计算2个日期之间相差的天数,忽略时分秒,如11-2号和11-5号之间,相差天数为3天
     *
     * @author fxsen 2014-12-12 下午5:04:26
     */
    public static int daysBetween(Date begin, Date end) {
        /**
         * 开始时间,忽略时分秒
         */
        Calendar beginCalendar = Calendar.getInstance();
        beginCalendar.setTime(begin);
        beginCalendar.set(Calendar.HOUR_OF_DAY, 12);
        beginCalendar.set(Calendar.MINUTE, 0);
        beginCalendar.set(Calendar.SECOND, 0);
        /**
         * 结束时间,忽略时分秒
         */
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(end);
        endCalendar.set(Calendar.HOUR_OF_DAY, 12);
        endCalendar.set(Calendar.MINUTE, 0);
        endCalendar.set(Calendar.SECOND, 0);

        return (int) ((endCalendar.getTimeInMillis() - beginCalendar.getTimeInMillis()) / ONE_DAY_MILLIONS);

    }

    /**
     * 根据日期 找到对应日期的 星期
     */
    public static String getDayOfWeekByDate(String date) {
        String dayOfweek = "-1";
        try {
            SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
            Date myDate = myFormatter.parse(date);
            SimpleDateFormat formatter = new SimpleDateFormat("E");
            String str = formatter.format(myDate);
            dayOfweek = str;

        } catch (Exception e) {
            System.out.println("错误!");
        }
        return dayOfweek;
    }

    /**
     * @return List<string>形式返回 (eg 00:00,01:00,...) Description:获取一天里面的24小时
     *         以List<string>形式返回 Date: 2014年2月20日 Time: 下午6:42:22
     * @author kong.degen
     */
    public static List<String> getDayOfHours() {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < 24; i++) {
            if (i < 10) {
                list.add("0" + i + ":00");
            } else {
                list.add(i + ":00");
            }
        }
        return list;
    }

    /**
     * 获取开始时间到现在的年份
     * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
     * @date: 2022/8/6 17:50
 	 * @param: end
 	 * @return: {@link List< String>}
    **/
    public static List<String> getYearList(String end) {
        List<String> list = new ArrayList<String>();
        Calendar calendar = Calendar.getInstance();
        boolean flag = true;
        while (flag) {
            list.add(calendar.get(Calendar.YEAR) + "");
            if (Integer.parseInt(end) == calendar.get(Calendar.YEAR)) {
                flag = false;
            }
            calendar.add(Calendar.YEAR, -1);

        }
        return list;
    }

    /**
     * 返回日期整点 例如 2014-10-06 13:36:45 返回 13:00 Date:
     * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
     * @date: 2022/8/6 17:50
 	 * @param: date
 	 * @return: {@link String}
    **/
    public static String getFullHour(Date date) {
        if (date == null) {
            return "null";
        }
        SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT_HOUR);
        return formatter.format(date);
    }

    /**
     * 获取传入时间、0时0分0秒
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static Date getDayNotHourMinuteSecond(Date date) throws ParseException {
        return new SimpleDateFormat(DEFAULT_DATE_FORMAT).parse(DateUtils.format(date, DEFAULT_DATE_FORMAT));
    }

    /*
     * 将返回的日志时间 转换成Date时间
     */
    public static Date getDateFromNgixDataString(String ngixDateString) {
        if (ngixDateString == null) {

        }
        ngixDateString = ngixDateString.trim();
        if (ngixDateString.equals("")) {

        }
        String regex = "[\\d]{1,2}/";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(ngixDateString);
        String day = "";
        String month = "";
        String year = "";
        String hour = "";
        String minute = "";
        String second = "";
        StringBuilder date = new StringBuilder();
        if (matcher.find()) {
            day = matcher.group();
            ngixDateString = matcher.replaceFirst("");
        }
        regex = "[a-zA-Z]+/";
        pattern = Pattern.compile(regex);
        matcher = pattern.matcher(ngixDateString);
        if (matcher.find()) {
            month = matcher.group();
            ngixDateString = matcher.replaceFirst("");
        }
        regex = "[\\d]{4}";
        pattern = Pattern.compile(regex);
        matcher = pattern.matcher(ngixDateString);
        if (matcher.find()) {
            year = matcher.group();
            ngixDateString = matcher.replaceFirst("");
        }
        regex = "[\\d]{1,2}";
        pattern = Pattern.compile(regex);
        matcher = pattern.matcher(ngixDateString);
        if (matcher.find()) {
            hour = matcher.group();
            ngixDateString = matcher.replaceFirst("");
        }
        regex = "[\\d]{1,2}";
        pattern = Pattern.compile(regex);
        matcher = pattern.matcher(ngixDateString);
        if (matcher.find()) {
            minute = matcher.group();
            ngixDateString = matcher.replaceFirst("");
        }
        regex = "[\\d]{1,2}";
        pattern = Pattern.compile(regex);
        matcher = pattern.matcher(ngixDateString);
        if (matcher.find()) {
            second = matcher.group();
            ngixDateString = matcher.replaceFirst("");
        }
        date.append(year).append("-").append(ENG_MONTH_MAP.get(month.toUpperCase())).append("-").append(day).append(" ")
                .append(hour).append(":").append(minute).append(":").append(second);

        return DateUtils.parseDate(date.toString().replace("/", ""));
    }

    // public static boolean dateBetween(Date begin,Date end){
    // return
    // }

    /**
     * 返回d1.before(d2-day)
     *
     * @param d1
     * @param d2
     * @param day
     * @return
     */
    public static boolean isBefore(Date d1, Date d2, int day) {
        Calendar c = Calendar.getInstance();
        c.setTime(d2);
        int day1 = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day1 - day);
        return d1.before(c.getTime());
    }

    /**
     * 获取本月1号0时0分0秒
     * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
     * @date: 2022/8/6 17:49
 	 * @return: {@link Date}
    **/
    public static Date getThisMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, 1);
        calendar.set(Calendar.HOUR, -12);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取当年1月1日 0:0:0
     * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
     * @date: 2022/8/6 17:49
 	 * @return: {@link Date}
    **/
    public static Date getThisYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        calendar.set(Calendar.DATE, 1);
        calendar.set(Calendar.HOUR, -12);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     * 返回年月日hash路径,例如 /2014/10/02
     * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
     * @date: 2022/8/6 17:49
 	 * @param: date
 	 * @return: {@link String}
    **/
    public static String getYYYYMMDDHash(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        StringBuilder sb = new StringBuilder();
        sb.append(File.separator);
        sb.append(calendar.get(Calendar.YEAR));
        sb.append(File.separator);
        sb.append(calendar.get(Calendar.MONTH));
        sb.append(File.separator);
        sb.append(calendar.get(Calendar.DAY_OF_MONTH));
        return sb.toString();
    }

    /**
     * 得到从今天开始的几个工作日集合
     * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
     * @date: 2022/8/6 17:48
 	 * @param: count  获取多少个工作日
 	 * @return: {@link List< Date>} 获取到的日期集合
    **/
    public static List<Date> getDates(int count) {

        List<Date> dates = new ArrayList<Date>();

        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        while (dates.size() < count) {
            int day = cal.get(Calendar.DAY_OF_WEEK);

            if (!(day == Calendar.SUNDAY || day == Calendar.SATURDAY)) {
                dates.add((Date) cal.getTime().clone());
            }
            cal.add(Calendar.DATE, 1);
        }
        return dates;

    }

    /**
     * 格式化毫秒
     * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
     * @date: 2022/8/6 17:48
 	 * @param: millis
 	 * @param: format
 	 * @return: {@link String}
    **/
    public static String formatMillis(long millis, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(millis);
        return formatter.format(calendar.getTime());
    }

    /**
     * 计算2个日期之前相差的时间,格式化返回天/时/分/秒
     * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
     * @date: 2022/8/6 17:48
 	 * @param: firstDate
 	 * @param: secondDate
 	 * @return: {@link long[]}
    **/
    public static long[] calcDayIntervalTime(Date firstDate, Date secondDate) {
        if (firstDate == null) {
            firstDate = new Date(0);
        }
        if (secondDate == null) {
            secondDate = new Date(0);
        }
        long firstTime = firstDate.getTime();
        long secondTime = secondDate.getTime();
        long intervalTime = Math.abs(firstTime - secondTime);
        long day = 0;
        long hour = 0;
        long minute = 0;
        long second = 0;
        day = intervalTime / ONE_DAY_MILLIONS;
        hour = (intervalTime - day * ONE_DAY_MILLIONS) / ONE_HOUR_MILLIONS;
        minute = (intervalTime - day * ONE_DAY_MILLIONS - hour * ONE_HOUR_MILLIONS) / ONE_MINUTE_MILLIONS;
        second = (intervalTime - day * ONE_DAY_MILLIONS - hour * ONE_HOUR_MILLIONS - minute * ONE_MINUTE_MILLIONS)
                / ONE_SECOND_MILLIOS;
        return new long[] { day, hour, minute, second };
    }

    /**
     * 获得当月第一天的日期
     * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
     * @date: 2022/8/6 17:48
 	 * @param: date
 	 * @return: {@link Date}
    **/
    public static Date getDateFromMonthFirstDay(Date date){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
        return c.getTime();
    }

    /**
     * 获得当月最后一天的日期
     * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
     * @date: 2022/8/6 17:47
 	 * @param: date
 	 * @return: {@link Date}
    **/
    public static Date getDateFromMonthLastDay(Date date){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        return c.getTime();
    }
    /**
     * 判定传入的日期的时分秒是否在2个时间之间,不看年月日
     * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
     * @date: 2022/8/6 17:47
 	 * @param: date
 	 * @param: beginDate
 	 * @param: endDate
 	 * @return: {@link boolean}
    **/
    public static boolean isTimeBetweenTime(Date date, Date beginDate, Date endDate) {
        boolean isBetween = false;
        if (date != null) {
            /**
             * 需要判定的时间
             */
            Calendar currentCalendar = Calendar.getInstance();
            currentCalendar.setTime(date);
            currentCalendar.set(2014, 1, 1);
            /**
             * 开始时间
             */
            Calendar beginCalendar = Calendar.getInstance();
            beginCalendar.setTime(beginDate);
            beginCalendar.set(2014, 1, 1);
            /**
             * 结束时间
             */
            Calendar endCalendar = Calendar.getInstance();
            endCalendar.setTime(endDate);
            endCalendar.set(2014, 1, 1);

            /**
             * 判定日期大小
             */
            if (currentCalendar.getTime().after(beginCalendar.getTime())
                    && currentCalendar.getTime().before(endCalendar.getTime())) {
                isBetween = true;
            }

        }
        return isBetween;
    }

    /**
     * 获取指定日期所在年份中的最后一天,如果date为空,则默认返回当前年份中的第一天
     * 返回格式只设置为"2015-12-31"年月日部分为正确,时分秒不可用或者不正确
     * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
     * @date: 2022/8/6 17:47
 	 * @param: date
 	 * @return: {@link Date}
    **/
    public static Date getLastDayOfYear(Date date) {
        if(date == null){
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.MONTH, Calendar.DECEMBER);
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            return calendar.getTime();
        }else{
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(Calendar.MONTH, Calendar.DECEMBER);
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            return calendar.getTime();
        }
    }

    /**
     * 获取指定日期所在年份中的第一天,如果date为空,则默认返回当前年份中的第一天
     * 返回格式只设置为"2015-01-01"年月日部分为正确,时分秒不可用或者不正确
     * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
     * @date: 2022/8/6 17:47
 	 * @param: date
 	 * @return: {@link Date}
    **/
    public static Date getFirstDayOfYear(Date date) {
        if(date == null){
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.MONTH, Calendar.JANUARY);
            calendar.set(Calendar.DATE, 1);
            return calendar.getTime();
        }else{
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(Calendar.MONTH, Calendar.JANUARY);
            calendar.set(Calendar.DATE, 1);
            return calendar.getTime();
        }
    }


    private static SimpleDateFormat FORMAT_0_0 = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
    private static SimpleDateFormat FORMAT_59_59 = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
    /**
     * 获取23：59：59，用于数据库查询时间区间
     * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
     * @date: 2022/8/6 17:46
 	 * @param: date
 	 * @return: {@link String}
    **/
    public static String getTwentyFour(Date date){
        return FORMAT_0_0.format(date);
    }
    /**
     * 获取凌晨00:00:00，用于数据库查询时间区间
     * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
     * @date: 2022/8/6 17:46
 	 * @param: date
 	 * @return: {@link String}
    **/
    public static String getBeforeDown(Date date){
        return FORMAT_0_0.format(date);
    }

    /**
     * 获取指定日期开始的时分秒
     * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
     * @date: 2022/8/6 17:46
 	 * @param: date
 	 * @return: {@link Date}
    **/
    public static Date getDayBegin(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date beginDate = calendar.getTime();
        return beginDate;
    }

    /**
     * 获取指定日期最后的时分秒
     * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
     * @date: 2022/8/6 17:46
 	 * @param: date
 	 * @return: {@link Date}
    **/
    public static Date getDayEnd(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        Date endDate = calendar.getTime();
        return endDate;
    }

    /**
     * (只比较年月日,时分秒无效或者不使用)获取指定时间相差天数(例如:begin:20160301 end:20160302,则返回1),如果开始时间或者结束时间为空或者开始时间在结束时间之后,则返回-1
     * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
     * @date: 2022/8/6 17:45
 	 * @param: begin 开始时间
 	 * @param: end  结束时间
 	 * @return: {@link int}
    **/
    public static int getDays(Date begin, Date end) {
        if (begin == null || end == null || begin.after(end)) {
            return -1;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(begin);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        begin = calendar.getTime();
        calendar.setTime(end);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        end = calendar.getTime();
        long intervalMilli = end.getTime() - begin.getTime();
        return (int) (intervalMilli / (24 * 60 * 60 * 1000));
    }

    public static Date getDate(int year, int month) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month - 1);
        return c.getTime();
    }
    /**
     * 返回startDate和endDate之间相差的月数
     * @param startDate
     * @param endDate
     * @return
     */
    public static Integer getDifMonth(Date startDate, Date endDate){
        Calendar start = Calendar.getInstance();
        Calendar end = Calendar.getInstance();
        start.setTime(startDate);
        end.setTime(endDate);
        int result = end.get(Calendar.MONTH) - start.get(Calendar.MONTH);
        int month = (end.get(Calendar.YEAR) - start.get(Calendar.YEAR)) * 12;
        return Math.abs(month + result);
    }

    /**
     * 通过统一的入口获取时间，这样方便调试不同时间点的问题、不用每次都修改电脑时间（关键是我的电脑修改不了--!）
     * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
     * @date: 2022/8/6 17:45
 	 * @return: {@link Date}
    **/
    public static Date getCurrentDate(){
//		String line = null;
//		InputStreamReader read = null;
//		try {
//			File f = new File("C:\\Users\\vip\\Desktop\\time.txt");
//			if (f.isFile() && f.exists()) {
//				read = new InputStreamReader(
//						new FileInputStream(f), "UTF-8");
//				BufferedReader reader = new BufferedReader(read);
//
//				while ((line = reader.readLine()) != null) {
//					if(line != null){
//						return parseDate(line, "yyyy-MM-dd HH:mm:ss");
//					}
//				}
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//		}finally {
//
//			try {
//				read.close();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
//		return null;
        return new Date();
    }
    public static void main(String[] args) {
        System.out.println(getDifMonth(parseDate("2021-01-01"), parseDate("2022-05-01")));
    }
}


/**
 * 逝去时间内部类
 */
class ElapsedTime {
    public Date getAfterMonth(GregorianCalendar g, int monthNum) {
        GregorianCalendar gc = (GregorianCalendar) g.clone();
        gc.add(Calendar.MONTH, monthNum);
        return gc.getTime();
    }
}
